import { Button, Card, message,  } from 'antd'
import TextEditor from 'Components/TextEditor'
// import createWriteBoardEditor from 'Components/TextEditor/extendMenu/WriteBoardMenu'
import React, { useRef, useState } from 'react'
import { Redirect } from 'react-router-dom'
import useStores from 'Stores'
import './index.scss'
import E from 'wangeditor'

const WriteBoard = () => {
  const [content, setContent] = useState('')
  const { post, root } = useStores()
  const ref = useRef<E>(new E('.editor'))

  if (!root.isLogin) {
    return <Redirect to="/home/post_page" />
  }

  const onPostArticle = async () => {
    const res = await post.writeArticle({
      content: content,
    })
    if (res.affectedRows === 1) {
      message.success('发表成功')
      setContent('')
      ref.current.txt.clear()
      return 
    }
    message.error(`注册失败！${res}`)
  }

  return (
    <>
      <Card className="write_board_content">
        <div id="edit">
          <TextEditor editor={ref.current} setState={setContent} />
        </div>
        <Button
          style={{ marginTop: 10 }}
          type="primary"
          onClick={onPostArticle}
        >
          发表
        </Button>
      </Card>
    </>
  )
}

export default WriteBoard
